<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="ActivityName" label="活动名称" v-show="!id">
				<uni-data-picker v-model="formData.ActivityName" collection="abu_activity"
					field="ActivityName as text,_id as value" trim="both"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="user_id" label="绑定的用户" v-show="!id">
				<uni-data-picker v-model="formData.user_id" collection="uni-id-users"
					field="username as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="Name" label="姓名" required>
				<uni-easyinput placeholder="参与者的真实姓名" v-model="formData.Name" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="Contact" label="联系方式" required>
				<uni-easyinput placeholder="联系方式，可以是电话号码或电子邮箱" v-model="formData.Contact" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="baoming_type" label="报名来源" v-show="!id">
				<uni-data-checkbox v-model="formData.baoming_type"
					:localdata="formOptions.baoming_type_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="RegistrationStatus" label="报名状态" v-show="!id">
				<uni-data-checkbox v-model="formData.RegistrationStatus"
					:localdata="formOptions.RegistrationStatus_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="Notes" label="备注">
				<uni-easyinput placeholder="关于参与者或报名的额外信息或特殊需求" v-model="formData.Notes" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="RegistrationDate" label="报名时间">
				<uni-datetime-picker return-type="timestamp" v-model="formData.RegistrationDate"></uni-datetime-picker>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/abu_baoming.js';

	const db = uniCloud.database();
	const dbCollectionName = 'abu_baoming';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		onLoad(e) {
			if (e && e.id) {
				this.id = e.id;
				this.formData.ActivityName = e.id;
				this.formData.baoming_type = 1;
				this.formData.RegistrationStatus = 1;
				const user_info = uniCloud.getCurrentUserInfo()
				// console.log(user_info);
				this.formData.user_id = user_info.uid

			}
		},
		data() {
			let formData = {
				"ActivityName": "",
				"user_id": "",
				"Name": "",
				"Contact": "",
				"baoming_type": null,
				"RegistrationStatus": null,
				"Notes": "",
				"RegistrationDate": null
			}
			return {
				id: '',
				formData,
				formOptions: {
					"baoming_type_localdata": [{
							"text": "线下报名",
							"value": 0
						},
						{
							"text": "APP报名",
							"value": 1
						}
					],
					"RegistrationStatus_localdata": [{
							"text": "已报名",
							"value": 0
						},
						{
							"text": "待审核",
							"value": 1
						},
						{
							"text": "已取消",
							"value": 2
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>